package com.pingbu.tijian.hospital.backapi.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pingbu.common.bean.ResultBean;
import com.pingbu.common.utils.PbSecretUtils;
import com.pingbu.tijian.hospital.backapi.bean.entity.StudentGroup;
import com.pingbu.tijian.hospital.backapi.bean.entity.UserInfo;
import com.pingbu.tijian.hospital.backapi.mapper.StudentGroupMapper;
import com.pingbu.tijian.hospital.backapi.mapper.UserInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentGroupService {

    private static Logger logger = LoggerFactory.getLogger(StudentGroupService.class);

    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private StudentGroupMapper studentGroupMapper;

    /**
     * 获取所有学生组
     * @param param
     * @return
     */
    public ResultBean<List<StudentGroup>> list(StudentGroup param){
        List<StudentGroup> lst1 = studentGroupMapper.list(param);

        // 完善 ，遍历 lst1 里面的 create_user_id ，更新 createUserRealname
        for(StudentGroup st: lst1){
            UserInfo ui1 = new UserInfo();
            ui1.setNoid(st.getCreate_user_id());
            UserInfo ui2 = userInfoMapper.get(ui1);
            if(ui2!=null){
                st.setCreateUserRealname(ui2.getRealname());
            }
        }

        return ResultBean.create(0,"success",lst1);
    }

    /**
     * 增加一个学生组
     */
    public ResultBean<Integer> add(StudentGroup param){

        //
        param.setNoid(PbSecretUtils.uuid());
        try {
            studentGroupMapper.insert(param);
        }catch (Exception err){
            return ResultBean.create(23,err.getMessage());
        }
        return ResultBean.create(0,"success");

    }

    public ResultBean<Integer> delete(StudentGroup param){
        Integer res = studentGroupMapper.delete(param);
        return ResultBean.create(0,"success",res);

    }

    public ResultBean<Integer> update(StudentGroup param){
        StudentGroup stuGroup = new StudentGroup();
        stuGroup.setStudent_group_name(param.getStudent_group_name());
        stuGroup.setOrder_numb(param.getOrder_numb());
        Integer res = studentGroupMapper.update(param);
        return ResultBean.create(0,"success",res);

    }

}
